﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Terraria.GameContent.UI.Elements;
using Terraria.Localization;
using Terraria.UI;
using Terraria.UI.Gamepad;

namespace Terraria.ModLoader.UI
{
	// Token: 0x020002AA RID: 682
	internal class UIMods : UIState
	{
		// Token: 0x06001B26 RID: 6950 RVA: 0x0042F164 File Offset: 0x0042D364
		public override void OnInitialize()
		{
			this.uIElement = new UIElement();
			this.uIElement.Width.Set(0f, 0.8f);
			this.uIElement.MaxWidth.Set(600f, 0f);
			this.uIElement.Top.Set(220f, 0f);
			this.uIElement.Height.Set(-220f, 1f);
			this.uIElement.HAlign = 0.5f;
			this.uIPanel = new UIPanel();
			this.uIPanel.Width.Set(0f, 1f);
			this.uIPanel.Height.Set(-110f, 1f);
			this.uIPanel.BackgroundColor = new Color(33, 43, 79) * 0.8f;
			this.uIPanel.PaddingTop = 0f;
			this.uIElement.Append(this.uIPanel);
			this.uiLoader = new UILoaderAnimatedImage(0.5f, 0.5f, 1f);
			this.modList = new UIList();
			this.modList.Width.Set(-25f, 1f);
			this.modList.Height.Set(-50f, 1f);
			this.modList.Top.Set(50f, 0f);
			this.modList.ListPadding = 5f;
			this.uIPanel.Append(this.modList);
			UIScrollbar uiscrollbar = new UIScrollbar();
			uiscrollbar.SetView(100f, 1000f);
			uiscrollbar.Height.Set(-50f, 1f);
			uiscrollbar.Top.Set(50f, 0f);
			uiscrollbar.HAlign = 1f;
			this.uIPanel.Append(uiscrollbar);
			this.modList.SetScrollbar(uiscrollbar);
			UITextPanel<string> uitextPanel = new UITextPanel<string>(Language.GetTextValue("tModLoader.ModsModsList"), 0.8f, true);
			uitextPanel.HAlign = 0.5f;
			uitextPanel.Top.Set(-35f, 0f);
			uitextPanel.SetPadding(15f);
			uitextPanel.BackgroundColor = new Color(73, 94, 171);
			this.uIElement.Append(uitextPanel);
			this.buttonEA = new UIColorTextPanel(Language.GetTextValue("tModLoader.ModsEnableAll"), Color.Green, 1f, false);
			this.buttonEA.Width.Set(-10f, 0.333333343f);
			this.buttonEA.Height.Set(25f, 0f);
			this.buttonEA.VAlign = 1f;
			this.buttonEA.Top.Set(-65f, 0f);
			this.buttonEA.OnMouseOver += UICommon.FadedMouseOver;
			this.buttonEA.OnMouseOut += UICommon.FadedMouseOut;
			this.buttonEA.OnClick += this.EnableAll;
			this.uIElement.Append(this.buttonEA);
			this.buttonDA = new UIColorTextPanel(Language.GetTextValue("tModLoader.ModsDisableAll"), Color.Red, 1f, false);
			this.buttonDA.CopyStyle(this.buttonEA);
			this.buttonDA.HAlign = 0.5f;
			this.buttonDA.OnMouseOver += UICommon.FadedMouseOver;
			this.buttonDA.OnMouseOut += UICommon.FadedMouseOut;
			this.buttonDA.OnClick += this.DisableAll;
			this.uIElement.Append(this.buttonDA);
			this.buttonRM = new UITextPanel<string>(Language.GetTextValue("tModLoader.ModsReloadMods"), 1f, false);
			this.buttonRM.CopyStyle(this.buttonEA);
			this.buttonRM.HAlign = 1f;
			this.buttonRM.OnMouseOver += UICommon.FadedMouseOver;
			this.buttonRM.OnMouseOut += UICommon.FadedMouseOut;
			this.buttonRM.OnClick += this.ReloadMods;
			this.uIElement.Append(this.buttonRM);
			this.buttonB = new UITextPanel<string>(Language.GetTextValue("UI.Back"), 1f, false);
			this.buttonB.CopyStyle(this.buttonEA);
			this.buttonB.Top.Set(-20f, 0f);
			this.buttonB.OnMouseOver += UICommon.FadedMouseOver;
			this.buttonB.OnMouseOut += UICommon.FadedMouseOut;
			this.buttonB.OnClick += UIMods.BackClick;
			this.uIElement.Append(this.buttonB);
			this.buttonOMF = new UITextPanel<string>(Language.GetTextValue("tModLoader.ModsOpenModsFolder"), 1f, false);
			this.buttonOMF.CopyStyle(this.buttonB);
			this.buttonOMF.HAlign = 0.5f;
			this.buttonOMF.OnMouseOver += UICommon.FadedMouseOver;
			this.buttonOMF.OnMouseOut += UICommon.FadedMouseOut;
			this.buttonOMF.OnClick += UIMods.OpenModsFolder;
			this.uIElement.Append(this.buttonOMF);
			Texture2D texture = Texture2D.FromStream(Main.instance.GraphicsDevice, Assembly.GetExecutingAssembly().GetManifestResourceStream("Terraria.ModLoader.UI.UIModBrowserIcons.png"));
			UIElement uielement = new UIElement();
			uielement.Width.Set(0f, 1f);
			uielement.Height.Set(32f, 0f);
			uielement.Top.Set(10f, 0f);
			for (int i = 0; i < 3; i++)
			{
				UICycleImage uicycleImage;
				if (i == 0)
				{
					uicycleImage = new UICycleImage(texture, 3, 32, 32, 102, 0, 2);
					uicycleImage.setCurrentState((int)this.sortMode);
					uicycleImage.OnClick += delegate(UIMouseEvent a, UIElement b)
					{
						this.sortMode = this.sortMode.NextEnum<ModsMenuSortMode>();
						this.updateNeeded = true;
					};
					uicycleImage.OnRightClick += delegate(UIMouseEvent a, UIElement b)
					{
						this.sortMode = this.sortMode.PreviousEnum<ModsMenuSortMode>();
						this.updateNeeded = true;
					};
				}
				else if (i == 1)
				{
					uicycleImage = new UICycleImage(texture, 3, 32, 32, 136, 0, 2);
					uicycleImage.setCurrentState((int)this.enabledFilterMode);
					uicycleImage.OnClick += delegate(UIMouseEvent a, UIElement b)
					{
						this.enabledFilterMode = this.enabledFilterMode.NextEnum<EnabledFilter>();
						this.updateNeeded = true;
					};
					uicycleImage.OnRightClick += delegate(UIMouseEvent a, UIElement b)
					{
						this.enabledFilterMode = this.enabledFilterMode.PreviousEnum<EnabledFilter>();
						this.updateNeeded = true;
					};
				}
				else
				{
					uicycleImage = new UICycleImage(texture, 5, 32, 32, 170, 0, 2);
					uicycleImage.setCurrentState((int)this.modSideFilterMode);
					uicycleImage.OnClick += delegate(UIMouseEvent a, UIElement b)
					{
						this.modSideFilterMode = this.modSideFilterMode.NextEnum<ModSideFilter>();
						this.updateNeeded = true;
					};
					uicycleImage.OnRightClick += delegate(UIMouseEvent a, UIElement b)
					{
						this.modSideFilterMode = this.modSideFilterMode.PreviousEnum<ModSideFilter>();
						this.updateNeeded = true;
					};
				}
				uicycleImage.Left.Set((float)(i * 36 + 8), 0f);
				this._categoryButtons.Add(uicycleImage);
				uielement.Append(uicycleImage);
			}
			UIPanel uipanel = new UIPanel();
			uipanel.Top.Set(0f, 0f);
			uipanel.Left.Set(-170f, 1f);
			uipanel.Width.Set(135f, 0f);
			uipanel.Height.Set(40f, 0f);
			uipanel.OnRightClick += delegate(UIMouseEvent a, UIElement b)
			{
				this.filterTextBox.SetText("");
			};
			uielement.Append(uipanel);
			this.filterTextBox = new UIInputTextField(Language.GetTextValue("tModLoader.ModsTypeToSearch"));
			this.filterTextBox.Top.Set(5f, 0f);
			this.filterTextBox.Left.Set(-160f, 1f);
			this.filterTextBox.Width.Set(160f, 0f);
			this.filterTextBox.Height.Set(20f, 0f);
			this.filterTextBox.OnTextChange += delegate(object a, EventArgs b)
			{
				this.updateNeeded = true;
			};
			uielement.Append(this.filterTextBox);
			this.SearchFilterToggle = new UICycleImage(texture, 2, 32, 32, 68, 0, 2);
			this.SearchFilterToggle.setCurrentState((int)this.searchFilterMode);
			this.SearchFilterToggle.OnClick += delegate(UIMouseEvent a, UIElement b)
			{
				this.searchFilterMode = this.searchFilterMode.NextEnum<SearchFilter>();
				this.updateNeeded = true;
			};
			this.SearchFilterToggle.OnRightClick += delegate(UIMouseEvent a, UIElement b)
			{
				this.searchFilterMode = this.searchFilterMode.PreviousEnum<SearchFilter>();
				this.updateNeeded = true;
			};
			this.SearchFilterToggle.Left.Set(545f, 0f);
			this._categoryButtons.Add(this.SearchFilterToggle);
			uielement.Append(this.SearchFilterToggle);
			this.buttonMP = new UITextPanel<string>(Language.GetTextValue("tModLoader.ModsModPacks"), 1f, false);
			this.buttonMP.CopyStyle(this.buttonOMF);
			this.buttonMP.HAlign = 1f;
			this.buttonMP.OnMouseOver += UICommon.FadedMouseOver;
			this.buttonMP.OnMouseOut += UICommon.FadedMouseOut;
			this.buttonMP.OnClick += UIMods.GotoModPacksMenu;
			this.uIElement.Append(this.buttonMP);
			this.uIPanel.Append(uielement);
			base.Append(this.uIElement);
		}

		// Token: 0x06001B27 RID: 6951 RVA: 0x00013639 File Offset: 0x00011839
		private static void BackClick(UIMouseEvent evt, UIElement listeningElement)
		{
			Main.PlaySound(11, -1, -1, 1, 1f, 0f);
			Main.menuMode = 0;
		}

		// Token: 0x06001B28 RID: 6952 RVA: 0x00013656 File Offset: 0x00011856
		private void ReloadMods(UIMouseEvent evt, UIElement listeningElement)
		{
			Main.PlaySound(10, -1, -1, 1, 1f, 0f);
			if (this.items.Count > 0)
			{
				ModLoader.Reload();
			}
		}

		// Token: 0x06001B29 RID: 6953 RVA: 0x00013680 File Offset: 0x00011880
		private static void OpenModsFolder(UIMouseEvent evt, UIElement listeningElement)
		{
			Main.PlaySound(10, -1, -1, 1, 1f, 0f);
			Directory.CreateDirectory(ModLoader.ModPath);
			Process.Start(ModLoader.ModPath);
		}

		// Token: 0x06001B2A RID: 6954 RVA: 0x000136AD File Offset: 0x000118AD
		private static void GotoModPacksMenu(UIMouseEvent evt, UIElement listeningElement)
		{
			if (!Interface.modsMenu.loading)
			{
				Main.PlaySound(12, -1, -1, 1, 1f, 0f);
				Main.menuMode = 10016;
			}
		}

		// Token: 0x06001B2B RID: 6955 RVA: 0x0042FAD0 File Offset: 0x0042DCD0
		private void EnableAll(UIMouseEvent evt, UIElement listeningElement)
		{
			Main.PlaySound(12, -1, -1, 1, 1f, 0f);
			foreach (UIModItem uimodItem in this.items)
			{
				uimodItem.Enable();
			}
		}

		// Token: 0x06001B2C RID: 6956 RVA: 0x0042FB38 File Offset: 0x0042DD38
		private void DisableAll(UIMouseEvent evt, UIElement listeningElement)
		{
			Main.PlaySound(12, -1, -1, 1, 1f, 0f);
			foreach (UIModItem uimodItem in this.items)
			{
				uimodItem.Disable();
			}
		}

		// Token: 0x06001B2D RID: 6957 RVA: 0x0042FBA0 File Offset: 0x0042DDA0
		public override void Update(GameTime gameTime)
		{
			base.Update(gameTime);
			if (this.needToRemoveLoading)
			{
				this.needToRemoveLoading = false;
				this.uIPanel.RemoveChild(this.uiLoader);
			}
			if (!this.updateNeeded)
			{
				return;
			}
			this.updateNeeded = false;
			this.filter = this.filterTextBox.currentString;
			this.modList.Clear();
			this.modList.AddRange(from item in this.items
			where item.PassFilters()
			select item);
		}

		// Token: 0x06001B2E RID: 6958 RVA: 0x0042FC38 File Offset: 0x0042DE38
		public override void Draw(SpriteBatch spriteBatch)
		{
			base.Draw(spriteBatch);
			for (int i = 0; i < this._categoryButtons.Count; i++)
			{
				if (this._categoryButtons[i].IsMouseHovering)
				{
					string text;
					switch (i)
					{
					case 0:
						text = this.sortMode.ToFriendlyString();
						break;
					case 1:
						text = this.enabledFilterMode.ToFriendlyString();
						break;
					case 2:
						text = this.modSideFilterMode.ToFriendlyString();
						break;
					case 3:
						text = this.searchFilterMode.ToFriendlyString();
						break;
					default:
						text = "None";
						break;
					}
					float x = Main.fontMouseText.MeasureString(text).X;
					Vector2 vector = new Vector2((float)Main.mouseX, (float)Main.mouseY) + new Vector2(16f);
					if (vector.Y > (float)(Main.screenHeight - 30))
					{
						vector.Y = (float)(Main.screenHeight - 30);
					}
					if (vector.X > (float)Main.screenWidth - x)
					{
						vector.X = (float)Main.screenWidth - x - 30f;
					}
					Utils.DrawBorderStringFourWay(spriteBatch, Main.fontMouseText, text, vector.X, vector.Y, new Color((int)Main.mouseTextColor, (int)Main.mouseTextColor, (int)Main.mouseTextColor, (int)Main.mouseTextColor), Color.Black, Vector2.Zero, 1f);
					return;
				}
			}
			UILinkPointNavigator.Shortcuts.BackButtonCommand = 1;
		}

		// Token: 0x06001B2F RID: 6959 RVA: 0x000136DA File Offset: 0x000118DA
		public override void OnActivate()
		{
			Main.clrInput();
			this.modList.Clear();
			this.items.Clear();
			this.loading = true;
			this.uIPanel.Append(this.uiLoader);
			this.Populate();
		}

		// Token: 0x06001B30 RID: 6960 RVA: 0x00013715 File Offset: 0x00011915
		internal void Populate()
		{
			if (SynchronizationContext.Current == null)
			{
				SynchronizationContext.SetSynchronizationContext(new SynchronizationContext());
			}
			Task.Factory.StartNew<LocalMod[]>(new Func<LocalMod[]>(ModLoader.FindMods)).ContinueWith(delegate(Task<LocalMod[]> task)
			{
				LocalMod[] result = task.Result;
				foreach (LocalMod mod in result)
				{
					UIModItem item = new UIModItem(mod);
					this.items.Add(item);
				}
				this.needToRemoveLoading = true;
				this.updateNeeded = true;
				this.loading = false;
			}, TaskScheduler.FromCurrentSynchronizationContext());
		}

		// Token: 0x04001852 RID: 6226
		private UIElement uIElement;

		// Token: 0x04001853 RID: 6227
		private UIPanel uIPanel;

		// Token: 0x04001854 RID: 6228
		private UILoaderAnimatedImage uiLoader;

		// Token: 0x04001855 RID: 6229
		private bool needToRemoveLoading;

		// Token: 0x04001856 RID: 6230
		private UIList modList;

		// Token: 0x04001857 RID: 6231
		private readonly List<UIModItem> items = new List<UIModItem>();

		// Token: 0x04001858 RID: 6232
		private bool updateNeeded;

		// Token: 0x04001859 RID: 6233
		public bool loading;

		// Token: 0x0400185A RID: 6234
		private UIInputTextField filterTextBox;

		// Token: 0x0400185B RID: 6235
		public UICycleImage SearchFilterToggle;

		// Token: 0x0400185C RID: 6236
		public ModsMenuSortMode sortMode;

		// Token: 0x0400185D RID: 6237
		public EnabledFilter enabledFilterMode;

		// Token: 0x0400185E RID: 6238
		public ModSideFilter modSideFilterMode;

		// Token: 0x0400185F RID: 6239
		public SearchFilter searchFilterMode;

		// Token: 0x04001860 RID: 6240
		internal readonly List<UICycleImage> _categoryButtons = new List<UICycleImage>();

		// Token: 0x04001861 RID: 6241
		internal string filter;

		// Token: 0x04001862 RID: 6242
		private UIColorTextPanel buttonEA;

		// Token: 0x04001863 RID: 6243
		private UIColorTextPanel buttonDA;

		// Token: 0x04001864 RID: 6244
		private UITextPanel<string> buttonRM;

		// Token: 0x04001865 RID: 6245
		private UITextPanel<string> buttonB;

		// Token: 0x04001866 RID: 6246
		private UITextPanel<string> buttonOMF;

		// Token: 0x04001867 RID: 6247
		private UITextPanel<string> buttonMP;
	}
}
